package com.xywh.micro_warehousing.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;
import com.baidu.mapapi.model.LatLng;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.xywh.micro_warehousing.R;
import com.xywh.micro_warehousing.di.component.DaggerDownMapShowComponent;
import com.xywh.micro_warehousing.di.module.DownMapShowModule;
import com.xywh.micro_warehousing.mvp.contract.DownMapShowContract;
import com.xywh.micro_warehousing.mvp.presenter.DownMapShowPresenter;
import com.xywh.micro_warehousing.mvp.ui.entity.DownMapShow;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;

import me.jessyan.armscomponent.commonres.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_DOWN_MAP_SHOW)
public class DownMapShowActivity extends BaseActivity<DownMapShowPresenter> implements DownMapShowContract.View, MKOfflineMapListener {

    FrameLayout layout;
    Double lat;
    Double lon;
    LatLng center;
    float zoom;
    MKOfflineMap mkOfflineMap;
    BaiduMap baiduMap;
    private MapView mMapView;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerDownMapShowComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .downMapShowModule(new DownMapShowModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_down_map_show; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_down_map_show));
        //getDownMapShowData();

        //调用离线地图，否则第二次进入会加载不到
        mkOfflineMap = new MKOfflineMap();
        mkOfflineMap.init(this);

        MapStatus.Builder builder = new MapStatus.Builder();

        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = intent.getExtras();
            lat = Double.valueOf("120.39472");
            lon = Double.valueOf("36.079773");
            Log.e("lon", lon + "");
            center = new LatLng(lat, lon);
            zoom = 13.0f;
        }
        builder.target(center).zoom(zoom);

        setMapCustomFile();

        mMapView = new MapView(this, new BaiduMapOptions());
        initView(this);

        baiduMap = mMapView.getMap();
        //默认显示普通地图
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));


        testDir();
    }

    private void testDir() {
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
            }
        }
    }

    // 设置地图config文件路径
    private void setMapCustomFile() {
        String str = Environment.getExternalStorageState().toString() + "/BaiduMap/vmp/";
        File file = new File(str);
        if (!(file.exists())) {
            try {
                new File(str).createNewFile();
                InputStream iinput = openFileInput(str + ".cfg");
                FileOutputStream output = new FileOutputStream(str);
                byte[] buffer = new byte[8192];
                int i = 0;
                while ((i = iinput.read(buffer)) > 0) {
                    output.write(buffer, 0, i);
                }
                output.close();
                iinput.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 初始化View
    private void initView(Context context) {
        layout = new FrameLayout(this);
        layout.addView(mMapView);
    }

    @Override
    protected void onPause() {
        super.onPause();

        mMapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

        mMapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mMapView.onDestroy();
    }

    @Override
    public void onGetOfflineMapState(int i, int i1) {

    }

    @Override
    public void getDownMapShowDataSuccess(DownMapShow entityData) {


    }

    private void getDownMapShowData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getDownMapShowData(map);
    }


    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
